package org.jeecg.common.modules.wx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.mapper.WxUserinfoMapper;
import org.jeecg.common.modules.wx.service.IWxUserinfoService;
import org.jeecg.common.modules.wx.vo.WxUserInfoReqVo;
import org.jeecg.common.util.enums.StatusEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: wx_userinfo
 * @Author: jeecg-boot
 * @Date:   2020-08-23
 * @Version: V1.0
 */
@Service
public class WxUserinfoServiceImpl extends ServiceImpl<WxUserinfoMapper, WxUserinfo> implements IWxUserinfoService {
    @Resource
    private WxUserinfoMapper wxUserinfoMapper;

    @Override
    public void unSub(String openid, String wxAccountId) {
        LambdaQueryWrapper<WxUserinfo> query = Wrappers.lambdaQuery();
        query.eq(WxUserinfo::getOpenid,openid);
        query.eq(WxUserinfo::getWxAccountId,wxAccountId);
        WxUserinfo exists = getOne(query);
        if (Objects.nonNull(exists)){
            exists.setSubscribe(StatusEnum.INVALID.getCode());
            exists.setUpdateTime(new Date());
            updateById(exists);
        }
    }

    @Override
    public WxUserinfo getByOpenid(String openid) {
        LambdaQueryWrapper<WxUserinfo> query = Wrappers.lambdaQuery();
        query.eq(WxUserinfo::getOpenid,openid);
        return getOne(query);
    }

    @Override
    public WxUserinfo getByUnionid(String unionid) {
        LambdaQueryWrapper<WxUserinfo> query = Wrappers.lambdaQuery();
        query.eq(WxUserinfo::getUnionid,unionid);
        return getOne(query);
    }

    @Override
    public WxUserinfo getBySmOpenid(String smOpenid) {
        LambdaQueryWrapper<WxUserinfo> query = Wrappers.lambdaQuery();
        query.eq(WxUserinfo::getSmOpenid,smOpenid);
        return getOne(query);
    }

    @Override
    public List<WxUserinfo> getByIds(Collection<String> ids) {
        return getBaseMapper().selectBatchIds(ids);
    }

    @Override
    public List<WxUserInfoReqVo> getWxUserInfos(String nickname, String mobile,String doctorName, Integer dataStart, Integer pageSize){
        return wxUserinfoMapper.selectWxUserInfo(nickname,mobile,doctorName,dataStart,pageSize);
    }

    @Override
    public int getCount(String nickname, String mobile,String doctorName){
        return wxUserinfoMapper.getCount(nickname,mobile,doctorName);
    }
}